package com.erp.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.order.api.domain.ErpSellerChildOrder;
import com.ruoyi.order.api.domain.dto.EvaluationDto;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.order.mapper.ErpSellerChildOrderMapper;
import com.erp.order.service.IErpSellerChildOrderService;
import com.ruoyi.common.core.web.oldDomain.BaseEntity;
import com.ruoyi.order.api.domain.ErpSellerChildOrder;
import com.ruoyi.order.api.domain.dto.EvaluationDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商户子订单Service业务层处理
 *
 * @author erp
 * @date 2023-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpSellerChildOrderServiceImpl implements IErpSellerChildOrderService {

    private final ErpSellerChildOrderMapper baseMapper;

    @Override
    public List<ErpSellerChildOrder> getChildOrderListByOrderId(Long orderId) {

        return baseMapper.selectList(new LambdaQueryWrapper<ErpSellerChildOrder>().eq(ErpSellerChildOrder::getOrderId,orderId));
    }

    /**
     * 根据子订单号获取子订单信息
     */
    @Override
    public ErpSellerChildOrder getChildOrderByOrderId(String childOrderId){
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerChildOrder>().eq(ErpSellerChildOrder::getChildOrderId,childOrderId));

    }

    @Override
    public Boolean isOrderPlacedForProduct(String productIds, Integer moduleReplace) {
        List<String> productIdList = Arrays.stream(productIds.split(",")).collect(Collectors.toList());
        return baseMapper.judgeOutOrders(moduleReplace, productIdList) > 0;
    }

    /**
     * 根据店铺cn码查看过去几天是否有订单
     * @param beforeDay
     * @param storeUserName
     * @param shieldProductIdList
     * @return
     */
    @Override
    public List<ErpSellerChildOrder> getOrdersByDay(Integer beforeDay, String storeUserName, List<String> shieldProductIdList){
        return baseMapper.getOrdersByDay(beforeDay, storeUserName, shieldProductIdList);
    }

    /**
     * 根据类型获取店铺运营数据
     * @param productId
     * @param dataType
     * @return
     */
    @Override
    public EvaluationDto getProductOperationsData(Long productId, String dataType){
        switch (dataType){
            case "1":
                return baseMapper.selectProductTips(productId);
            case "2":
                return baseMapper.selectWishlists(productId);
            case "3":
                return baseMapper.selectVisitors(productId);
            case "0":
                return baseMapper.selectOutOrder(productId);
        }
        return null;
    }

    /**
     * 判断上一单是否是测评单
     * @param storeUserName
     * @param skuCode
     * @return
     */
    @Override
    public Integer selectIsEvaluationBySkuCode(String storeUserName, String skuCode){
        return baseMapper.selectIsEvaluationBySkuCode(storeUserName, skuCode);
    }

//
//    /**
//     * 查询商户子订单
//     */
//    @Override
//    public ErpSellerChildOrderVo queryById(Long id) {
//        return baseMapper.selectVoById(id);
//    }
//
//    /**
//     * 查询商户子订单列表
//     */
//    @Override
//    public TableDataInfo<ErpSellerChildOrderVo> queryPageList(ErpSellerChildOrderBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ErpSellerChildOrder> lqw = buildQueryWrapper(bo);
//        Page<ErpSellerChildOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
//    }
//
//    /**
//     * 查询商户子订单列表
//     */
//    @Override
//    public List<ErpSellerChildOrderVo> queryList(ErpSellerChildOrderBo bo) {
//        LambdaQueryWrapper<ErpSellerChildOrder> lqw = buildQueryWrapper(bo);
//        return baseMapper.selectVoList(lqw);
//    }
//
//    private LambdaQueryWrapper<ErpSellerChildOrder> buildQueryWrapper(ErpSellerChildOrderBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<ErpSellerChildOrder> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getOrderId() != null, ErpSellerChildOrder::getOrderId, bo.getOrderId());
//        lqw.eq(StringUtils.isNotBlank(bo.getChildOrderId()), ErpSellerChildOrder::getChildOrderId, bo.getChildOrderId());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductUnit()), ErpSellerChildOrder::getProductUnit, bo.getProductUnit());
//        lqw.eq(StringUtils.isNotBlank(bo.getSendGoodsOperator()), ErpSellerChildOrder::getSendGoodsOperator, bo.getSendGoodsOperator());
//        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), ErpSellerChildOrder::getSkuCode, bo.getSkuCode());
//        lqw.eq(StringUtils.isNotBlank(bo.getSnapshotId()), ErpSellerChildOrder::getSnapshotId, bo.getSnapshotId());
//        lqw.eq(StringUtils.isNotBlank(bo.getSnapshotSmallPhotoPath()), ErpSellerChildOrder::getSnapshotSmallPhotoPath, bo.getSnapshotSmallPhotoPath());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductImgUrl()), ErpSellerChildOrder::getProductImgUrl, bo.getProductImgUrl());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductSnapUrl()), ErpSellerChildOrder::getProductSnapUrl, bo.getProductSnapUrl());
//        lqw.eq(bo.getGoodsPrepareTime() != null, ErpSellerChildOrder::getGoodsPrepareTime, bo.getGoodsPrepareTime());
//        lqw.like(StringUtils.isNotBlank(bo.getLogisticsServiceName()), ErpSellerChildOrder::getLogisticsServiceName, bo.getLogisticsServiceName());
//        lqw.eq(StringUtils.isNotBlank(bo.getBuyerMemo()), ErpSellerChildOrder::getBuyerMemo, bo.getBuyerMemo());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsWarehouseType()), ErpSellerChildOrder::getLogisticsWarehouseType, bo.getLogisticsWarehouseType());
//        lqw.eq(StringUtils.isNotBlank(bo.getTags()), ErpSellerChildOrder::getTags, bo.getTags());
//        lqw.eq(StringUtils.isNotBlank(bo.getAlreadyTaxed()), ErpSellerChildOrder::getAlreadyTaxed, bo.getAlreadyTaxed());
//        lqw.eq(StringUtils.isNotBlank(bo.getExtendMap()), ErpSellerChildOrder::getExtendMap, bo.getExtendMap());
//        lqw.eq(StringUtils.isNotBlank(bo.getAfflicateFeeRate()), ErpSellerChildOrder::getAfflicateFeeRate, bo.getAfflicateFeeRate());
//        lqw.eq(StringUtils.isNotBlank(bo.getIssueModel()), ErpSellerChildOrder::getIssueModel, bo.getIssueModel());
//        lqw.eq(StringUtils.isNotBlank(bo.getIssueStatus()), ErpSellerChildOrder::getIssueStatus, bo.getIssueStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getIssueTime()), ErpSellerChildOrder::getIssueTime, bo.getIssueTime());
//        lqw.eq(StringUtils.isNotBlank(bo.getEscrowFeeRate()), ErpSellerChildOrder::getEscrowFeeRate, bo.getEscrowFeeRate());
//        lqw.eq(StringUtils.isNotBlank(bo.getFrozenStatus()), ErpSellerChildOrder::getFrozenStatus, bo.getFrozenStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getFundStatus()), ErpSellerChildOrder::getFundStatus, bo.getFundStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getInitOrderAmount()), ErpSellerChildOrder::getInitOrderAmount, bo.getInitOrderAmount());
//        lqw.eq(bo.getInitOrderCent() != null, ErpSellerChildOrder::getInitOrderCent, bo.getInitOrderCent());
//        lqw.eq(bo.getInitOrderCentFactor() != null, ErpSellerChildOrder::getInitOrderCentFactor, bo.getInitOrderCentFactor());
//        lqw.eq(bo.getInitOrderCurrencyDescriptionId() != null, ErpSellerChildOrder::getInitOrderCurrencyDescriptionId, bo.getInitOrderCurrencyDescriptionId());
//        lqw.eq(StringUtils.isNotBlank(bo.getInitOrderCurrencyCode()), ErpSellerChildOrder::getInitOrderCurrencyCode, bo.getInitOrderCurrencyCode());
//        lqw.eq(StringUtils.isNotBlank(bo.getLoanAmount()), ErpSellerChildOrder::getLoanAmount, bo.getLoanAmount());
//        lqw.eq(bo.getLoanCent() != null, ErpSellerChildOrder::getLoanCent, bo.getLoanCent());
//        lqw.eq(bo.getLoanCentFactor() != null, ErpSellerChildOrder::getLoanCentFactor, bo.getLoanCentFactor());
//        lqw.eq(bo.getLoanCurrencyDescriptionId() != null, ErpSellerChildOrder::getLoanCurrencyDescriptionId, bo.getLoanCurrencyDescriptionId());
//        lqw.eq(StringUtils.isNotBlank(bo.getLoanCurrencyCode()), ErpSellerChildOrder::getLoanCurrencyCode, bo.getLoanCurrencyCode());
//        lqw.eq(StringUtils.isNotBlank(bo.getLoanTime()), ErpSellerChildOrder::getLoanTime, bo.getLoanTime());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsAmount()), ErpSellerChildOrder::getLogisticsAmount, bo.getLogisticsAmount());
//        lqw.eq(bo.getLogisticsCent() != null, ErpSellerChildOrder::getLogisticsCent, bo.getLogisticsCent());
//        lqw.eq(bo.getLogisticsCentFactor() != null, ErpSellerChildOrder::getLogisticsCentFactor, bo.getLogisticsCentFactor());
//        lqw.eq(bo.getLogisticsDescriptionId() != null, ErpSellerChildOrder::getLogisticsDescriptionId, bo.getLogisticsDescriptionId());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsCurrencyCode()), ErpSellerChildOrder::getLogisticsCurrencyCode, bo.getLogisticsCurrencyCode());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsType()), ErpSellerChildOrder::getLogisticsType, bo.getLogisticsType());
//        lqw.eq(bo.getLotNum() != null, ErpSellerChildOrder::getLotNum, bo.getLotNum());
//        lqw.eq(StringUtils.isNotBlank(bo.getOrderSortId()), ErpSellerChildOrder::getOrderSortId, bo.getOrderSortId());
//        lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), ErpSellerChildOrder::getOrderStatus, bo.getOrderStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductAttributes()), ErpSellerChildOrder::getProductAttributes, bo.getProductAttributes());
//        lqw.eq(bo.getProductCount() != null, ErpSellerChildOrder::getProductCount, bo.getProductCount());
//        lqw.eq(bo.getProductId() != null, ErpSellerChildOrder::getProductId, bo.getProductId());
//        lqw.like(StringUtils.isNotBlank(bo.getProductName()), ErpSellerChildOrder::getProductName, bo.getProductName());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductPrice()), ErpSellerChildOrder::getProductPrice, bo.getProductPrice());
//        lqw.eq(bo.getProductCent() != null, ErpSellerChildOrder::getProductCent, bo.getProductCent());
//        lqw.eq(bo.getProductCentFactor() != null, ErpSellerChildOrder::getProductCentFactor, bo.getProductCentFactor());
//        lqw.eq(bo.getProductDescriptionId() != null, ErpSellerChildOrder::getProductDescriptionId, bo.getProductDescriptionId());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductCurrencyCode()), ErpSellerChildOrder::getProductCurrencyCode, bo.getProductCurrencyCode());
//        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerChildOrder::getRevision, bo.getRevision());
//        return lqw;
//    }
//
//    /**
//     * 新增商户子订单
//     */
//    @Override
//    public Boolean insertByBo(ErpSellerChildOrderBo bo) {
//        ErpSellerChildOrder add = BeanUtil.toBean(bo, ErpSellerChildOrder.class);
//        validEntityBeforeSave(add);
//        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
//        return flag;
//    }
//
//    /**
//     * 修改商户子订单
//     */
//    @Override
//    public Boolean updateByBo(ErpSellerChildOrderBo bo) {
//        ErpSellerChildOrder update = BeanUtil.toBean(bo, ErpSellerChildOrder.class);
//        validEntityBeforeSave(update);
//        return baseMapper.updateById(update) > 0;
//    }
//
//    /**
//     * 保存前的数据校验
//     */
//    private void validEntityBeforeSave(ErpSellerChildOrder entity) {
//        //TODO 做一些数据校验,如唯一约束
//    }
//
//    /**
//     * 批量删除商户子订单
//     */
//    @Override
//    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
//        if (isValid) {
//            //TODO 做一些业务上的校验,判断是否需要校验
//        }
//        return baseMapper.deleteBatchIds(ids) > 0;
//    }
//
//    @Override
//    public List<ErpCollectProductSkuPackageInfo> getChildOrderProduct(Long orderId) {
//        //1、查找所有的子订单的产品的Sku_code
//        List<ErpCollectProductSkuPackageInfo> skuPackageInfoList = baseMapper.getChildOrderSkuCode(orderId);
//        return skuPackageInfoList;
//    }
//
//    /**
//     * 通过子订单id获取子订单列表
//     *
//     * @param childIds
//     * @return
//     */
//    @Override
//    public List<DeclareProductDTOS> selectChildOrderListByChildOrderIds(String[] childIds) {
//        return baseMapper.selectChildOrderListByChildOrderIds(childIds);
//    }
//
//    @Override
//    public List<String> getChildOrderIdProduct(Long orderId) {
//        return baseMapper.getChildOrderIdProduct(orderId);
//    }
//
    @Override
    public Map<String, Object> getOrderCountByCode(String skuCode) {
        Map<String, Object> map = new HashMap<>();
        String threeDay = baseMapper.selectSkuCountByTime(skuCode, "0");
        String sevenDay = baseMapper.selectSkuCountByTime(skuCode, "1");
        String oneMonth = baseMapper.selectSkuCountByTime(skuCode, "2");
        map.put("threeDay",threeDay);
        map.put("sevenDay",sevenDay);
        map.put("oneMonth",oneMonth);
        String violationRate = baseMapper.selectViolationRate(skuCode);
        if (violationRate == null) {
            violationRate = "0.00";
        }
        map.put("violationRate",violationRate);
        return map;
    }

    @Override
    public List<ErpSellerChildOrder> getChildByOrder(Long orderId) {
        return baseMapper.selectList(new LambdaQueryWrapper<ErpSellerChildOrder>().eq(ErpSellerChildOrder::getOrderId, orderId));
    }
//
//
//    @Override
//    public List<ErpNeedPriceProductInfoDto> selectNeedPriceRiseProductInfo(String storeUserName, String zeroHour) {
//        return baseMapper.selectNeedPriceRiseProductInfo(storeUserName,zeroHour);
//    }
//
//    @Override
//    public List<ErpNeedPriceProductInfoDto> selectNeedPriceOffProductInfo(Long storeId, Integer priceOffPeriod, Integer statisticalPeriod) {
//        return baseMapper.selectNeedPriceOffProductInfo(storeId,priceOffPeriod,statisticalPeriod);
//    }

}
